/* ------------------------------------------------------------------------*
 *
 * ------------------------------------------------------------------------*/
#include "os_obj.h"
#include "at_hal.h"
#include "str_ext.h"

/*****************************************************************************
 * @brief   at_event_send
 *****************************************************************************/
void at_event_send(uint16_t event)
{
	osSignalSet(os_obj.tid_esp, event);
}

/*****************************************************************************
 * @brief   at_hal_init
 *****************************************************************************/
void at_hal_init(void)
{
	at_port.os_event_send = at_event_send;
	at_port.os_event_base = 0x100;

	at_port.rcv_end_time_sv = 10;
	at_port.rcv_end_keep_sv = 65535;
	at_port.master_wait_time_sv = 500;
}

/*****************************************************************************
 * @brief   Send a string.
 * @param   pData
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void at_hal_string_send(const char *pData)
{
	at_port.fun->snd_string(pData);
}

/*****************************************************************************
 * @brief   Send data.
 * @param   pData
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void at_hal_data_send(const uint8_t *pData, uint16_t Len)
{
	at_port.fun->snd_data(pData, Len);
}

/*****************************************************************************
 * @brief   Port state check.
 * @param   none
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
uint8_t at_hal_idle_check(void)
{
	return at_port.fun->idle_check();
}

/*****************************************************************************
 * @brief   Port state reset.
 * @param   none
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void at_hal_rcv_clr(void)
{
	set_u8(at_port.rx_buff, 0, at_port.rx_buff_size);
	at_port.fun->rcv_st_clr();
	osSignalClear(os_obj.tid_esp, 0xFFFF);
}

/*****************************************************************************
 * @brief   	os event sync .
 * @param   time out cnt
 * @return  0-event  1-timeout
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
OS_SYNC_Type at_event_sync(uint32_t ms_timeout)
{
	osEvent Event;

	Event = osSignalWait(0, ms_ticks(ms_timeout));

	if (Event.status == osEventSignal)
	{
		if (COM_EVENT_RCVEND == (Event.value.signals & 0xFF))
		{
			return WIFI_SYNC_ST_RCVEND;
		}
	}

	return WIFI_SYNC_ST_TIMEOUT;
}

/*****************************************************************************
 * @brief   	os delay .
 * @param   time out cnt
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void at_delay(uint32_t ms_Time)
{
	osDelay(ms_ticks(ms_Time));
}
